﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;
using Terraria.UI;

namespace MoreEvent.Common.Systems
{
    public class EventTextSystem : ModSystem
    {
        public static List<EventText> ActiveTexts = [];

        public override void PostUpdateEverything()
        {
            for (int i = ActiveTexts.Count - 1; i >= 0; i--)
            {
                ActiveTexts[i].Update();
                if (ActiveTexts[i].ShouldRemove)
                {
                    ActiveTexts.RemoveAt(i);
                }
            }
        }

        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int mouseTextIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
            if (mouseTextIndex != -1)
            {
                layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer(
                    "MoreEvent: Event Text",
                    delegate
                    {
                        DrawEventTexts();
                        return true;
                    },
                    InterfaceScaleType.UI)
                );
            }
        }

        private static void DrawEventTexts()
        {
            foreach (var text in ActiveTexts)
            {
                text.Draw();
            }
        }

        public static void ShowEventText(string text, Color color, int displayTime = 180)
        {
            ActiveTexts.Add(new EventText(text, color, displayTime));
        }
    }

    public class EventText(string text, Color color, int displayTime)
    {
        public string Text { get; set; } = text;
        public Color Color { get; set; } = color;
        public int Timer { get; set; } = 0;
        public int DisplayTime { get; set; } = displayTime;
        public bool ShouldRemove => Timer >= DisplayTime;
        private const int FadeInTime = 30;   
        private const int FadeOutTime = 30;

        public void Update()
        {
            Timer++;
        }

        public void Draw()
        {
            float alpha = 1f;
            if (Timer < FadeInTime)
            {
                alpha = Timer / (float)FadeInTime;
            }
            else if (Timer > DisplayTime - FadeOutTime)
            {
                alpha = (DisplayTime - Timer) / (float)FadeOutTime;
            }
            var font = FontAssets.DeathText.Value;
            Vector2 textSize = font.MeasureString(Text);
            Vector2 position = new Vector2(Main.screenWidth / 2, Main.screenHeight / 3) - textSize / 2;          
            Color shadowColor = Color.Black * alpha * 0.5f;
            Vector2 shadowOffset = new(2, 2);
            Main.spriteBatch.DrawString(font, Text, position + shadowOffset, shadowColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
            Color drawColor = Color * alpha;
            Main.spriteBatch.DrawString(font, Text, position, drawColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
        }
    }
}